package com.blaze.framework.core.boot.config;

import com.blaze.framework.core.boot.argumentresolver.PageArgumentResolver;
import com.blaze.framework.core.boot.argumentresolver.TokenUserIdArgumentResolver;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * spring mvc 配置
 *
 * @author created by Zhou Zongbo on 2025/5/26.
 */
@AutoConfiguration
public class SpringMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        // 添加参数解析器
        argumentResolvers.add(new TokenUserIdArgumentResolver());
        argumentResolvers.add(new PageArgumentResolver());
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 设置默认编码为UTF-8
        converters.stream().filter(MappingJackson2HttpMessageConverter.class::isInstance)
                .findFirst()
                .ifPresent(converter -> ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8));
    }

    @Override
    public void addFormatters(@NonNull FormatterRegistry registry) {
        // 添加格式化处理器
    }
}
